use dta/panel_all, clear

/* only for descriptives (jan 28 2015) */

gen totalvotes_first=totalvotes_mun if round==1
replace totalvotes_first=. if round==2


/* MAKING FINAL ROUND VARIABLES */

gen turnout_mun_final=turnout_mun
replace turnout_mun_final=fut_turnout if fut_turnout!=.   /* replacing with second round turnout, if that exists */

gen totalvotes_final=totalvotes_mun
replace totalvotes_final=fut_totalvotes_mun if fut_totalvotes_mun!=.   /* replacing with second round turnout, if that exists */


/* KEEPING ONLY ONE ROUND OF MUNICIP ELECTIONS AND MERGING PR PERIOD */
keep if round==1

append using dta\storting1921_municipality
append using dta\storting1924_municipality
append using dta\storting1927_municipality
append using dta\storting1930_municipality
append using dta\storting1933_municipality
append using dta\storting1936_municipality

*gen turnout=.
*replace turnout=turnout_mun_final if year<1920
*replace turnout=approvedvotesoverall/eligibleoverall if year>1920

/* CREATING DISTRICT LEVEL VARIABLES */

egen SMD_district=max(valgkrets), by(knr)   /* valgkrets is between 101 and 2002 pre-reform and between 1 and 31 post-reform */
egen PR_district=min(valgkrets), by(knr)   /* valgkrets is between 101 and 2002 pre-reform and between 1 and 31 post-reform */

** oslo (5), drammen (2), kristiansand (2) stavanger (2), bergen (4), trondheim (4)
** ... consist of multiple districts within a municipality. We do not have the possibility to get post-reform data on that level
** ... so we lose these 19 districts. Leaving us with 126-19-12=95 districts.
** some municipalities are split after reform , need to manually find their SMD_district 

sort knr year
li knr SMD_district if SMD_district<100

replace SMD_district=101 if knr==126 /* 126 mysen was part of 125 eidsberg up to 1920 */
replace SMD_district=203 if knr==213 /* 213 ski was part of 212 kr�kstad up to 1931 */
replace SMD_district=203 if knr==222 /* 222 nordre h�land was part of 221 h�land */
replace SMD_district=203 if knr==225
replace SMD_district=203 if knr==228
*replace SMD_district=XXX if knr==301  /* oslo */
replace SMD_district=406 if knr==435
*replace SMD_district=XXX if knr==602 /* drammen */
replace SMD_district=902 if knr==917
*replace SMD_district=XXX if knr==1001
*replace SMD_district=XXX if knr==1103
replace SMD_district=1103 if knr==1125
replace SMD_district=1103 if knr==1127
replace SMD_district=1103 if knr==1144
replace SMD_district=1104 if knr==1151
replace SMD_district=1105 if knr==1156
replace SMD_district=1105 if knr==1158
replace SMD_district=1204 if knr==1256
replace SMD_district=1204 if knr==1260
replace SMD_district=1204 if knr==1262
*replace SMD_district=XXX if knr==1301
replace SMD_district=1404 if knr==1435
replace SMD_district=1404 if knr==1436
replace SMD_district=1502 if knr==1518
replace SMD_district=1504 if knr==1547
*replace SMD_district= if knr==1601
replace SMD_district=1602 if knr==1611
replace SMD_district=1602 if knr==1613
replace SMD_district=1603 if knr==1637
replace SMD_district=1603 if knr==1639
replace SMD_district=1604 if knr==1641
replace SMD_district=1604 if knr==1642
replace SMD_district=1604 if knr==1643
*replace SMD_district= if knr==1701
replace SMD_district=1703 if knr==1739
replace SMD_district=1703 if knr==1740
replace SMD_district=1703 if knr==1741
replace SMD_district=1801 if knr==1801
replace SMD_district=1802 if knr==1803
replace SMD_district=1802 if knr==1823
replace SMD_district=1802 if knr==1825
replace SMD_district=1802 if knr==1829
replace SMD_district=1802 if knr==1831
replace SMD_district=1804 if knr==1854
replace SMD_district=1805 if knr==1856
replace SMD_district=1805 if knr==1861
replace SMD_district=1805 if knr==1863
replace SMD_district=1806 if knr==1869
replace SMD_district=1806 if knr==1871
replace SMD_district=1806 if knr==1873
replace SMD_district=1901 if knr==1912
replace SMD_district=1901 if knr==1913
replace SMD_district=1902 if knr==1916
replace SMD_district=1902 if knr==1918
replace SMD_district=1902 if knr==1919
replace SMD_district=1903 if knr==1923
replace SMD_district=1904 if knr==1939
replace SMD_district=1904 if knr==1940

drop if SMD_district<100  /* these are the multiple-districts municipalities post-reform */
drop if SMD_district>3000  /* these are the multiple-districts municipalities pre-reform */

/* Lillehammer, Hamar, Gj�vik og Kongsvinger delt i to */
drop if valgkrets==400 
drop if valgkrets==490
drop if valgkrets==500

/* extra district was added in Romsdal, and then municipalities appear to be reshuffled from 1915 to 1918 */
drop if valgkrets==1501
drop if valgkrets==1502
drop if valgkrets==1503
drop if valgkrets==1504
drop if valgkrets==1505
drop if valgkrets==1506

/* extra district was added in Troms, and then municipalities appear to be slightly reshuffled from 1915 to 1918 */
drop if valgkrets==1901
drop if valgkrets==1902
drop if valgkrets==1903
drop if valgkrets==1904

replace eligibleoverall=eligible1overall if year<1920   /* eligibleoverall is a variable existing only for PR, eligible1overall is the first round elig in SMD */
replace approvedvotesoverall=totalvotes_final if year<1920 /* approvedvotesoverall is a variable existing only for PR, totalvotes_final is the final round no. votes in SMD */

drop if knr==1280 /* aarstad part of Bergen from 1.7.1915 */

drop if SMD_district==690  /* h�nefoss and notodden belongs to different post-reform districts (and counties) */
drop if SMD_district==892  /* brevik and holmestrand belongs to different post-reform districts (and counties) */
drop if SMD_district==1001  /* kristiansand consist of two districts */

drop _merge
sort knr

save dta/Turnout_District_Level_Before_Collapse, replace
collapse (mean) PR_district (mean) margin (sum) eligibleoverall (sum) approvedvotesoverall (sum) totalvotes_first, by(SMD_district year)

*bysort PR_district: tab SMD_district

gen turnout=approvedvotesoverall/eligibleoverall

xtset SMD_district year
gen xx=1
bysort SMD_district: egen zz=total(xx)
keep if zz==10

drop xx zz

preserve
use "dta\Storting1921",clear
gen PR_district=valgkrets
gen magnitude=rep
gen electorate1921=eligibleoverall       /* this is electorate in first PR election */
collapse magnitude electorate, by(PR_district)
sort PR_district
save "dta\magnitudePR",replace
restore
sort PR_district
merge PR_district using dta/magnitudePR

replace magnitude=1 if year<1920

/* creating var. for electorate based on the PR districts */
bysort PR_district year: egen electoratePR=sum(eligibleoverall) 

gen electorate=.
replace electorate=eligibleoverall if year<1920  /* this variable captures no. of eligible voters at the SMD level */
replace electorate=electoratePR if year>1920  /* this variable captures no. of eligible voters at the PR level */

gen voters_per_MP=electorate/magnitude

drop if year==.
sort SMD_d year
*browse year SMD_district PR_district magnitude eligibleoverall electoratePR electorate voters_per_MP


/* CREATING VARIOUS VARIABLES */

gen SMD=0
replace SMD=1 if year<1920

bysort SMD_district: egen SMD_margin_pre=mean(margin)  /* AVG. DIFFERENCE BETWEEN WINNER AND RUNNER UP IN FIRST ROUND IN SMD PERIOD */
xtile SMD_margin_quantile=SMD_margin_pre, nq(2)
tab SMD_margin_quantile, gen(d_SMD_margin_quantile)

bysort SMD_district: egen magnitude_post=max(magnitude)  /* post-reform magnitude */

sort SMD_district year
gen ch_turnout=turnout-l3.turnout
gen ch_magnitude=magnitude-l3.magnitude
gen ch_voters_per_MP=(voters_per_MP-l3.voters_per_MP)/1000

gen d_SMD_margin_quantile2Xch_voters=d_SMD_margin_quantile2*ch_voters

drop d_SMD_margin_quantile1  /*reference category */

tab year, gen(d_year)
tab SMD_district, gen(d_SMD_district)

gen close=SMD_margin_pre<0.149
gen reform=year>1920
gen small=magnitude_post<5.5

gen closeXsmall=close*small
gen closeXreform=close*reform
gen smallXreform=small*reform
gen closeXsmallXreform=close*small*reform

keep if year<1930 /* electoral cartels introduced */
save dta/Turnout_District_Level, replace
